/*
 * Copied from InstaCalc Bookmarklet
 */

function addCSS(url){
  var headID = document.getElementsByTagName("head")[0];
  var cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = url;
  cssNode.media = 'screen';
  headID.appendChild(cssNode);
}

function toggleItem(id){
  var item = document.getElementById(id);
  if(item){
    if (item.style.display == "none"){
      item.style.display = "";
    }
    else{
      item.style.display = "none";
    } 
  }
}

function showItem(id){
  try{
    var item = document.getElementById(id);
    if(item){
        item.style.display = "";
    }
  }
  catch(e){
  
  }
}

(function(){
  var iframe_url = "http://awesometext.googlecode.com/git/bookmarklet/iframe.html?x=" + Math.random();
  var css_url = "http://awesometext.googlecode.com/git/bookmarklet/bookmarklet.css";
  var existing_iframe = document.getElementById('awesometext_bookmarklet_iframe');
  
  if (existing_iframe){
    showItem('awesometext_bookmarklet');
    existing_iframe.src = iframe_url;
    return;
  }
  
  addCSS(css_url);
 
  var div = document.createElement("div");
  div.id = "awesometext_bookmarklet";
  
  var str = "";
  str += "<table id='awesometext_bookmarklet_table' valign='top' width='320' cellspacing='0' cellpadding='0'><tr><td width ='300' height='200px'>";
  str += "<iframe frameborder='0' scrolling='no' name='awesometext_bookmarklet_iframe' id='awesometext_bookmarklet_iframe' src='" + iframe_url + "' width='300px' height='205px' style='textalign:right; backgroundColor: white;'></iframe>";
  str += "</td><td onClick='toggleItem(\"awesometext_bookmarklet\");' style='background: #FFDDDD;' title='click to close window' valign='top' align='center' width='20px'>";
  str += "<a href='javascript:void(0);' style='width:100%; text-align: middle; color: #FF0000; font-family: Arial;'>x</a>";
  str += "</td></tr></table>";
  
  div.innerHTML = str;
  
  document.body.insertBefore(div, document.body.firstChild);
})()
